home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / PRMDash.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  5.0 KB  |  213 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                PRMDash.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWSTDEF_H
  13. #include "FWStdDef.h"
  14. #endif
  15.  
  16. #ifndef FWPRIDEB_H
  17. #include "FWPriDeb.h"
  18. #endif
  19.  
  20. #ifdef FW_BUILD_MAC        // Macintosh-only
  21.  
  22. #ifndef _ODMATH_
  23. #include <ODMath.h>
  24. #endif
  25.  
  26. #ifndef PRMDASH_H
  27. #include "PRMDash.h"
  28. #endif
  29.  
  30. //========================================================================================
  31. //    RunTime Info
  32. //========================================================================================
  33.  
  34. #ifdef FW_BUILD_MAC
  35. #pragma segment FWGraphics_MacDash
  36. #endif
  37.  
  38. //========================================================================================
  39. // class FW_CPrivMacDashDraw
  40. //========================================================================================
  41.  
  42. struct SStyleInfo
  43. {
  44.     short fDashCount;
  45.     short fDashData[6];
  46. };
  47.  
  48. static SStyleInfo gDashData[4] = 
  49. {
  50.     {    2,    {    18, 5                }    },    /* PS_DASH            */    
  51.     {    2,    {    3, 3                }    },    /* PS_DOT            */    
  52.     {    4,    {    9, 6, 3, 5            }    },    /* PS_DASHDOT        */    
  53.     {    6,    {    9, 3, 3, 3, 3, 3    }    }    /* PS_DASHDOTDOT    */    
  54. };
  55.  
  56. const int kStyleDashCount = sizeof(gDashData) / sizeof(gDashData[0]);
  57.  
  58. //----------------------------------------------------------------------------------------
  59. //    FW_CPrivMacDashDraw::FW_CPrivMacDashDraw
  60. //----------------------------------------------------------------------------------------
  61.  
  62. FW_CPrivMacDashDraw::FW_CPrivMacDashDraw(FW_EStyleDash dash) :
  63.     fCurSegN(0),
  64.     fCurSegDraw(TRUE)
  65. {
  66.     fIsOpaque  = (dash & FW_kOpaque) != 0;
  67.     short nDash = dash & ~FW_kOpaque;
  68.  
  69.     FW_ASSERT(nDash >= FW_kDash);
  70.     FW_ASSERT(nDash <= FW_kDashDotDot);
  71.  
  72.     fDashCount        = gDashData[nDash - 1].fDashCount;
  73.     fDashDistances    = gDashData[nDash - 1].fDashData;
  74.     
  75.     if (fIsOpaque)
  76.     {
  77.         ::GetForeColor(&fForeColor);
  78.         ::GetBackColor(&fBackColor);
  79.     }
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    FW_CPrivMacDashDraw::~FW_CPrivMacDashDraw
  84. //----------------------------------------------------------------------------------------
  85.  
  86. FW_CPrivMacDashDraw::~FW_CPrivMacDashDraw()
  87. {
  88.     // Restore the colors
  89.     if (fIsOpaque)
  90.     {
  91.         ::RGBForeColor(&fForeColor);
  92.         ::RGBBackColor(&fBackColor);
  93.     }
  94. }
  95.  
  96. //----------------------------------------------------------------------------------------
  97. //    FW_CPrivMacDashDraw::LineTo
  98. //----------------------------------------------------------------------------------------
  99.  
  100. void FW_CPrivMacDashDraw::LineTo(short h, short v)
  101. {
  102.     PenState ps;
  103.     ::GetPenState(&ps);
  104.  
  105.     short dh = h - ps.pnLoc.h;
  106.     short dv = v - ps.pnLoc.v;
  107.     
  108.     if ((ps.pnSize.h > 1 || ps.pnSize.v > 1) || (dh == 0 && dv == 0))
  109.     {
  110.         ::LineTo(h, v);
  111.         return;
  112.     }
  113.  
  114.     InitAdjust(dh, dv);
  115.  
  116.     short steps    = FW_Maximum(FW_Absolute((int)dh), FW_Absolute((int)dv));
  117.     
  118.     ODFixed fxHStep    = ODFixedDivide(ODIntToFixed(dh), ODIntToFixed(steps));
  119.     ODFixed fxVStep    = ODFixedDivide(ODIntToFixed(dv), ODIntToFixed(steps));
  120.     
  121.     ODFixed fxLength;
  122.     if (dh == 0)
  123.         fxLength = ODIntToFixed(FW_Absolute((int)dv));
  124.     else if (dv == 0)
  125.         fxLength = ODIntToFixed(FW_Absolute((int)dh));
  126.     else
  127.     {
  128.         ODWide w;
  129.         w.lo = 0;
  130.         w.hi = dh * dh + dv * dv;
  131.         fxLength = ODWideSquareRoot(&w);
  132.     }
  133.  
  134.     ODFixed fxStep = ODFixedDivide(fxLength, ODIntToFixed(steps));
  135.  
  136.     ODFixed fxDist = 0;
  137.     ODFixed fxHCur = ODIntToFixed(ps.pnLoc.h);
  138.     ODFixed fxVCur = ODIntToFixed(ps.pnLoc.v);
  139.  
  140.     ODFixed fxCurSeg = ODIntToFixed(fDashDistances[fCurSegN]);
  141.  
  142.     while (steps -- > 0)
  143.     {
  144.         fxHCur += fxHStep;
  145.         fxVCur += fxVStep;
  146.         fxDist += fxStep;
  147.  
  148.         if (fxDist >= fxCurSeg)
  149.         {
  150.             DrawOneSegment(ODFixedRound(fxHCur), ODFixedRound(fxVCur));
  151.  
  152.             fCurSegDraw    = !fCurSegDraw;
  153.             fCurSegN    = (fCurSegN + 1) % fDashCount;
  154.             fxDist        = 0;
  155.             fxCurSeg    = ODIntToFixed(fDashDistances[fCurSegN]);
  156.         }
  157.     }
  158.  
  159.     // Draw the last segment - this also finalizes the pen position
  160.     DrawOneSegment(h, v);
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. //    FW_CPrivMacDashDraw::InitAdjust
  165. //----------------------------------------------------------------------------------------
  166.  
  167. void FW_CPrivMacDashDraw::InitAdjust(short dh, short dv)
  168. {
  169.     fAdjustH = 0;
  170.     fAdjustV = 0;
  171.  
  172.     if (dv == 0)
  173.     {
  174.         if (dh > 0)
  175.             fAdjustH = -1;
  176.         else if(dh < 0)
  177.             fAdjustH = 1;
  178.     }
  179.     else if (dh == 0)
  180.     {
  181.         if (dv > 0)
  182.             fAdjustV = -1;
  183.         else if(dv < 0)
  184.             fAdjustV = 1;
  185.     }
  186. }
  187.  
  188. //----------------------------------------------------------------------------------------
  189. //    FW_CPrivMacDashDraw::DrawOneSegment
  190. //----------------------------------------------------------------------------------------
  191.  
  192. void FW_CPrivMacDashDraw::DrawOneSegment(short h, short v)
  193. {
  194. #ifdef FW_DEBUG
  195.     PenState ps;
  196.     ::GetPenState(&ps);
  197. #endif
  198.  
  199.     if (fIsOpaque)
  200.     {
  201.         ::RGBForeColor(fCurSegDraw ? &fForeColor : &fBackColor);
  202.         ::LineTo(h + fAdjustH, v + fAdjustV);
  203.     }
  204.     else if(fCurSegDraw)
  205.     {
  206.         ::LineTo(h + fAdjustH, v + fAdjustV);
  207.     }
  208.  
  209.     ::MoveTo(h, v);
  210. }
  211.  
  212. #endif // FW_BUILD_MAC
  213.